import { Injectable } from '@angular/core';
import { CanActivate } from "@angular/router";
import { AuthTokenService } from '@services/auth-token.service';
import { CommonResponse,VerificationType,ToastColor,CommonService } from '@services/common-service.service';
import { Router,ActivatedRouteSnapshot,RouterStateSnapshot } from '@angular/router';

@Injectable()
export class LoginGuard implements CanActivate{

    constructor(
        private authTokenService:AuthTokenService,
        private commonService:CommonService,
        private router: Router){
    }

    canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot){
        let loggedIn:boolean = true;
        if(!this.authTokenService.getUserToken()){
            if(this.router.url != '/auth/signin-password'
                && this.router.url != "/auth/signin-sms"
                && this.router.url != "/auth/signin-fingerprint"
                && this.router.url != "/auth/signin-first"){
                this.commonService.commonToast(ToastColor.warning,'User information has expired,please log in again');
                this.router.navigate(['/auth/signin-password']);
            } 
            loggedIn = false;
            console.log("no login");
        }
        return loggedIn;
    }
}